/**
 * 所有标签页
 * page所有标签
 */
import 'package:flutter/material.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/live/allTag.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/addTagModel.dart';
import 'package:qgchat/widgets/noData.dart';

class AllTag extends StatefulWidget {
  AllTag(this.zid, {Key key}) : super(key: key);

  final int zid;

  @override
  _AllTagState createState() => _AllTagState();
}

class _AllTagState extends State<AllTag> {
  AllTagBloc bloc;

  @override
  void initState() {
    super.initState();
    if (mounted) {
      bloc = BlocProvider.of<AllTagBloc>(context);
      bloc.allyingxiang(widget.zid);
    }
  }

  // 生成appbar
  PreferredSize appBarHandle() {
    return PreferredSize(
      preferredSize: Size.fromHeight(px(88)),
      child: AppBar(
        title: Text(
          "所有标签",
          style: TextStyle(
            color: Colors.black,
            fontSize: px(34),
            fontWeight: FontWeight.bold,
          ),
        ),
        centerTitle: true,
      ),
    );
  }

  // 生成body
  Widget bodyHandle() {
    return Container(
      child: StreamBuilder(
        stream: bloc.tagStream,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            List<AllTagModel> allTag = (snapshot.data as List<AllTagModel>);
            if (allTag.isNotEmpty) {
              return Container(
                width: w100(context),
                height: h100(context),
                padding: EdgeInsets.symmetric(horizontal: px(50), vertical: px(30)),
                color: Colors.white,
                child: Wrap(
                  spacing: px(20),
                  runSpacing: px(18),
                  children: allTag.map((AllTagModel item) {
                    return tag(item);
                  }).toList(),
                ),
              );
            } else {
              return NoData('暂时还没有标签哦~');
            }
          } else {
            return Container();
          }
        },
      ),
    );
  }

  Widget tag(AllTagModel item) {
    return Container(
      margin: EdgeInsets.only(right: px(15)),
      padding: EdgeInsets.symmetric(horizontal: px(20), vertical: px(12)),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(px(23)),
        color: col("f7d767"),
      ),
      child: Text(
        "${item.usertagName} ${item.num}",
        style: TextStyle(
          height: 0.99,
          color: Colors.white,
          fontSize: px(24),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarHandle(),
      body: bodyHandle(),
    );
  }
}
